package Configs;

import java.io.File;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.ext.DatabaseFileLockedException;
import com.db4o.ext.DatabaseReadOnlyException;
import com.db4o.ext.Db4oIOException;
import com.db4o.ext.IncompatibleFileFormatException;
import com.db4o.ext.OldFormatException;
import models.ChiTietGia;
import models.ChuNhaTro;
import models.GiaPhong;
import models.HoaDon;
import models.HopDong;
import models.LoaiPhong;
import models.NguoiThue;
import models.NhaTro;
import models.NhanVien;
import models.PhongTro;
import models.TaiKhoan;

public class Config {

    private ObjectContainer db;

    public Config() {
        this.reConfig();
    }
    
    public void reConfig()
    {
        try {

            EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
            //- Nguoi thue
            config.common().objectClass(NguoiThue.class).cascadeOnUpdate(true);
            //     config.common().objectClass(NguoiThue.class).cascadeOnDelete(true);
            //- Nhan vien

            config.common().objectClass(NhanVien.class).cascadeOnUpdate(true); 
            config.common().objectClass(NhanVien.class).cascadeOnDelete(true);
            //-Tai Khoan
            config.common().objectClass(TaiKhoan.class).cascadeOnUpdate(true); 
            config.common().objectClass(TaiKhoan.class).cascadeOnDelete(true); 

            config.common().objectClass(NhanVien.class).cascadeOnUpdate(true);
            //    config.common().objectClass(NhanVien.class).cascadeOnDelete(true); 

            //- Chu nha tro
            config.common().objectClass(ChuNhaTro.class).cascadeOnUpdate(true);
            //   config.common().objectClass(ChuNhaTro.class).cascadeOnDelete(true); 
            //- Nha tro
            config.common().objectClass(NhaTro.class).cascadeOnUpdate(true);
           // config.common().objectClass(NhaTro.class).cascadeOnDelete(true); 
            //- Phong tro
            config.common().objectClass(PhongTro.class).cascadeOnUpdate(true);
            //   config.common().objectClass(PhongTro.class).cascadeOnDelete(true); 
            //- Hop Dong
            config.common().objectClass(HopDong.class).cascadeOnUpdate(true);
            //    config.common().objectClass(HopDong.class).cascadeOnDelete(true); 
            //- Hoa don
            config.common().objectClass(HoaDon.class).cascadeOnUpdate(true);
            //   config.common().objectClass(HoaDon.class).cascadeOnDelete(true); 
            //- Loai phong
            config.common().objectClass(LoaiPhong.class).cascadeOnUpdate(true);
            //   config.common().objectClass(LoaiPhong.class).cascadeOnDelete(true); 
            //- Gia phong
           config.common().objectClass(GiaPhong.class).cascadeOnUpdate(true);
            //   config.common().objectClass(GiaPhong.class).cascadeOnDelete(true); 
            //- Chi tiet gia
            config.common().objectClass(ChiTietGia.class).cascadeOnUpdate(true);
            //   config.common().objectClass(ChiTietGia.class).cascadeOnDelete(true); 

            config.common().optimizeNativeQueries(false);
            //Khoi tao
            File currentDirFile = new File(".");
            String currentPathProject = currentDirFile.getAbsolutePath();
            this.db = Db4oEmbedded.openFile(config, currentPathProject.substring(0, currentPathProject.length() - 1) + "src/database/containers.dbo");

            System.out.println("Open connect to db4o successsfully!");
        } catch (Db4oIOException | DatabaseFileLockedException | IncompatibleFileFormatException | OldFormatException | DatabaseReadOnlyException ex) {
            System.out.println("Create connect to db4o failed. Cause: " + ex.getMessage());
        }
    }

    public ObjectContainer getConn() {
        return this.db;
    }

    public void closeConn() {
        if (this.db.close()) {
            System.out.println("Closed connect to DB4O!");
        }
    }

    public boolean isConn() {
        return this.db.close();
    }
}
